import java.util.Scanner;

public class test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入数组的长度: ");
        int length = scanner.nextInt();

        int[] nums = new int[length];
        System.out.println("请逐个输入数组的元素:");
        for (int i = 0; i < length; i++) {
            System.out.print("元素 " + (i + 1) + ": ");
            nums[i] = scanner.nextInt();
        }

        scanner.close();

        int maxSum = findMaxSubarraySum(nums);
        System.out.println("数组中子数组的最大和是: " + maxSum);
    }

    public static int findMaxSubarraySum(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int currentMax = nums[0];
        int globalMax = nums[0];

        for (int i = 1; i < nums.length; i++) {
            currentMax = Math.max(nums[i], currentMax + nums[i]);
            if (currentMax > globalMax) {
                globalMax = currentMax;
            }
        }

        return globalMax;
    }
}
